package com.servlet.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.servlet.service.impl.AccountServiceImpl;
import com.servlet.service.impl.CarrierServiceImpl;
import com.servlet.service.impl.TradeLogServiceImpl;
import com.servlet.vo.bean.Account;
import com.servlet.vo.bean.Carrier;
import com.servlet.vo.bean.TradeLog;

public class TradeLogAction extends HttpServlet {
	
//	private ICarrierService carrierService;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		boolean flag = false;
		String op = request.getParameter("op");
		try {
			if(null != op){
				if("AD".equals(op)){// 添加
					
//					flag = new ConsumDetailService().add(request);
					
					flag = new TradeLogServiceImpl().save(request);
					if(flag){
						String acc = request.getParameter("acc");
						if("1".equals(acc)){
							response.sendRedirect("AccountAction?op=SH");
						}else{
							response.sendRedirect("AccountAction?op=SH&id="+acc);
						}
						System.out.println("新增记账记录成功");
					}else{
						System.out.println("记账出错");
					}
					
				}else if("SH".equals(op)){// 展示记账页面
					
					String id = request.getParameter("id");// 账户id
					if(null != id && !"".equals(id)){
						Account acc = new AccountServiceImpl().getObjById(Long.parseLong(id));
						List<Carrier> carries = new CarrierServiceImpl().getObjByAccountId(Long.parseLong(id));
						request.setAttribute("carries", carries);
						request.setAttribute("acc", acc);
					}else{
						// 跳转参数错误页面
						
					}
					request.getRequestDispatcher("/WEB-INF/templates/frontpage/jizhang.jsp").forward(request, response);
					
				}else if("LI".equals(op)){// 获取账单列表
					//  acc:账户id（必传）   carr:载体id（选传） 
					List<TradeLog> objs = new TradeLogServiceImpl().getList(request);
					request.setAttribute("objs", objs);
					request.getRequestDispatcher("/WEB-INF/templates/frontpage/zhangdan.jsp").forward(request, response);
					
				}else if("OBJS".equals(op)){// 修改
					
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
